﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft Corp." file="HpcSchedulerServiceCredentials.cs">
//   All rights reserved. See License.txt in the project root for license information.
// </copyright>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace HpcSchedulerManagement
{
    using System;
    using System.Net;
    using System.Security.Cryptography.X509Certificates;

    using Common;

    public class HpcSchedulerServiceCredentials : IHpcSchedulerServiceCredentials
    {
        public HpcSchedulerServiceCredentials(string adminLogin, string adminPassword, string sslCertificateThumbprint)
        {
            this.AdminLogin = adminLogin;
            this.AdminPassword = adminPassword;

            this.Certificate = CertificateHelper.FindCertificate(X509FindType.FindByThumbprint, sslCertificateThumbprint);
            if (this.Certificate == null)
            {
                throw new ArgumentException("Invalid certificate thumbprint", "sslCertificateThumbprint");
            }
        }

        public string AdminLogin { get; private set; }

        public string AdminPassword { get; private set; }

        public X509Certificate2 Certificate { get; private set; }

        public WebRequest Sign(WebRequest request)
        {
            request.Credentials = new NetworkCredential(this.AdminLogin, this.AdminPassword);

            var httpWebRequest = request as HttpWebRequest;
            if (httpWebRequest != null)
            {
                httpWebRequest.ClientCertificates.Add(this.Certificate);
            }

            return request;
        }
    }
}